﻿<%@ Page Language="C#" %>
<script runat="server">
    private string _root = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/x-javascript";
        _root = Request.ApplicationPath.Length <= 1 ? "" : Request.ApplicationPath;
        if (Application["TemplateName"] != null)
        {
            ChangeTemplate();
        }
    }

    private void ChangeTemplate()
    {
        string strScript = "var sys = document.getElementById('sysCss');";
        strScript += "var main = document.getElementById('mainCss');";
        strScript += " var path = '" + Root + "/Template/tELemsun/" 
            + Application["TemplateName"] + "';";
        strScript += "if (sys != null) sys.href = path + '/system.css';";
        strScript += "if (main != null) main.href = path + '/main.css';";
        
        ScriptManager.RegisterStartupScript(this.Page, Page.GetType(),
                "changeTemplate", strScript, true);
    }
    
    public string Root
    {
        get 
        {
            return _root;
        }
    }
</script>
var cp = new Ext.state.CookieProvider({
    });
Ext.chart.Chart.CHART_URL =  '<%= Root %>/script/resources/charts.swf';
Ext.BLANK_IMAGE_URL = '<%= Root %>/script/resources/images/default/s.gif';
Ext.QuickTips.init();
var CATEGORY = {};
CATEGORY.UPDATE = 'UPDATE';
CATEGORY.DELETE = 'DELETE';
CATEGORY.INSERT = 'INSERT';
CATEGORY.READONLY = 'READONLY';
CATEGORY.CREATE = 'CREATE';

//统一的发送到服务器的类
function JsonRequest() {
    this.params = {};
    if(cp!=null)
    {
        this.params.XProject =cp.get("UserName");
    }
    else
    {
        this.params.XProject ="";
    }
    this.category = CATEGORY.READONLY;
}
var _urlReg = /\.+\.json\.*/;
Ext.apply(Ext, {
    Root:'<%= Root %>',
    getUrl:function(url){
        if(url == null || url == '') return this.Root;
        url = url.replace('\\', '/');
        if(url.search(/\//) == 0)
        {
            return this.Root + url;
        }
        return this.Root + '/' + url;
    },
    getClassUrl:function(className, methodName) {
        var classUrl = className.replace('.', '/');
        var key = (new Date()).format('U');
        return this.getUrl(classUrl + '/' + methodName + '.json?token=' + key);
    }
});

Ext.apply(Ext.tree.TreeLoader, {
    processResponse : function(response, node, callback, scope){
        var json = response.responseText;
        try {
            var o = response.responseData || Ext.decode(json);
            var o = o.ResponseObject;
            node.beginUpdate();

            for(var i = 0, len = o.length; i < len; i++){
                var n = this.createNode(o[i]);
                if(n){
                    node.appendChild(n);
                }
            }
            node.endUpdate();
            this.runCallback(callback, scope || node, [node]);
        }catch(e){
            this.handleFailure(response);
        }
    }
});
Ext.Ajax.on('beforerequest', function(conn, op){
    if(op.jsonData == null) {
    	
    	if(op.params instanceof Object && op.params.category != null ){
    		op.jsonData = op.params;
    	}
    	else {
	        var re = new JsonRequest();
	        re.params = op.params;
	        op.jsonData = re;
        }
    }
});

function changeTemplate() {
    var sys = document.getElementById('sysCss');
    var main = document.getElementById('mainCss');
    
    var path = '<%=Root%>'+'/Template/tELemsun/'+'<%=Application["TemplateName"]%>';
        
    if (sys != null) sys.href = path + "/system.css';";
    if (main != null) main.href = path + "/main.css';";
}